package com.anli.Controller;

import com.anli.Service.EmptService;
import com.anli.Utils.JwtUtils;
import com.anli.pojo.Emp;
import com.anli.pojo.Result;
import io.jsonwebtoken.Jwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmptService emptService;
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
//        log.info("员工登录"+emp);
        System.out.println(emp);
        Emp e = emptService.login(emp);
        if(e != null){
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("username",e.getUsername());
            claims.put("name",e.getName());
            String token = JwtUtils.generateJwt(claims);

            return Result.success(token);
        }
        return Result.error("用户名或密码错误");
    }
}
